﻿using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace ZhongKeSite.Common.Helper
{
    public class EnumHelper
    {
        /// <summary>
        /// 通过Index得到枚举
        /// </summary>
        /// <param name="T"></param>
        /// <param name="indexValue"></param>
        /// <returns></returns>
        public static T GetEnumByIndex<T>(int indexValue)
        {
            return (T)Enum.ToObject(typeof(T), indexValue);
        }

        /// <summary>
        /// 通过Index得到枚举
        /// </summary>
        /// <param name="T"></param>
        /// <param name="valueString"></param>
        /// <returns></returns>
        public static T GetEnumByName<T>(string value)
        {
            return (T)Enum.Parse(typeof(T), value,true);
        }

        /// <summary>
        /// 得到所选枚举 Text
        /// </summary>
        /// <param name="T"></param>
        /// <param name="itemValue"></param>
        /// <returns></returns>
        public static string GetSelectListItemName(Type T, string itemValue)
        {
            IEnumerable<SelectListItem> selectListItems = GetDDLFromEnum(T, false, null, null);
            if (selectListItems != null)
            {
                var item = selectListItems.Where(s => s.Value == itemValue).FirstOrDefault();
                if (item != null) return item.Text;
            }
            return string.Empty;
        }

        /// <summary>
        /// 得到枚举下拉 Text:Description  Value = Value 数字
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<int> GetEnumValues(Type T)
        {
            List<int> list = new List<int>();
            foreach (var e in Enum.GetValues(T))
            {
                list.Add(Convert.ToInt32(e));
            }
            return list;
        }

        /// <summary>
        /// 得到枚举下拉 Text:Description  Value = Value 数字
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<SelectListItem> GetDDLFromEnum(Type T)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var e in Enum.GetValues(T))
            {
                SelectListItem m = new SelectListItem();
                object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objArr != null && objArr.Length > 0)
                {
                    DescriptionAttribute da = objArr[0] as DescriptionAttribute;
                    m.Text = da.Description;
                }
                m.Value = Convert.ToInt32(e).ToString();
                list.Add(m);
            }
            return list;
        }
        /// <summary>
        /// 得到枚举下拉 Text:Description  Value = Value 字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<SelectListItem> GetSelectListFromEnum(Type T,bool isAll = false)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var e in Enum.GetValues(T))
            {
                SelectListItem m = new SelectListItem();
                object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objArr != null && objArr.Length > 0)
                {
                    DescriptionAttribute da = objArr[0] as DescriptionAttribute;
                    m.Text = da.Description;
                }
                m.Value = e.ToString();
                list.Add(m);
            }
            if(isAll)
            {
                list.Insert(0, new SelectListItem() { Value = "",Text = "--全部--" });
            }
            return list;
        }
        /// <summary>
        /// 取枚举中的值与描述作为下拉数据源
        /// </summary>
        /// <param name="T"></param>
        /// <param name="isValue"></param>
        /// <param name="otherItem"></param>
        /// <returns></returns>
        public static IEnumerable<SelectListItem> GetDDLFromEnum(Type T, bool isValue = false, SelectListItem otherItem = null, Func<string, bool> exp = null)
        {
            var dll = new List<SelectListItem>();
            string v = string.Empty;
            var fields = T.GetType().GetFields();
            var list = Enum.GetNames(T).ToList();
            if (exp != null)
            {
                list = list.Where(exp).ToList();
            }
            list.ForEach(s =>
            {
                v = s;
                var fi = T.GetField(s);
                var attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                var vname = attrs == null || attrs.Length == 0 ? s : ((DescriptionAttribute)attrs[0]).Description;
                if (isValue)
                {
                    v = ((int)Enum.Parse(T, s)).ToString();
                }
                dll.Add(new SelectListItem { Text = vname, Value = v });
            });

            if (otherItem != null)
            {
                dll.Insert(0, otherItem);
            }
            return dll;
        }

        /// <summary>
        /// 获取枚举描述值
        /// </summary>
        /// <param name="enumValue">此处限制enumValue为int类型</param>
        /// <returns></returns>
        public static string GetEnumDescription(Type T,int enumValue)
        {
            string value = enumValue.ToString();

            foreach (var e in Enum.GetValues(T))
            {
                if ((int)e== enumValue)
                {
                    object[] objs = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                    if (objs == null || objs.Length == 0)    //当描述属性没有时，直接返回名称
                    {
                        return value;
                    }
                    DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
                    return descriptionAttribute.Description;
                }
            }
            return "";
        }
    }
}
